package database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 
 * @author ayoubfalah
 *
 */
public class DatabaseHandler extends SQLiteOpenHelper{
	
	private static final int DATABASE_VERSION = 1;	
	private static final String DATABASE_NAME = "login_Data_Manager.db";
	private static final String TABLE_NAME = "players";
	
	// players table columns name
	private static final String KEY_ID = "_id";
	private static final String KEY_NAME = "name";
	private static final String KEY_PASSWORD = "password";
	
	public DatabaseHandler(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		
		String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
				+ KEY_ID + " INTEGER PRIMARY KEY,"
				+ KEY_NAME + " TEXT,"	
				+ KEY_PASSWORD + " TEXT"+ ")";
		
		/*String query = String.format
				("CREATE TABLE %s (%s INTEGER PRIMARY KEY, %s TEXT, %s TEXT)", TABLE_NAME, KEY_ID, KEY_NAME,
						KEY_PASSWORD);*/
		db.execSQL(CREATE_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		String DROP_TABLE = "DROP IF NOT EXISTS " + TABLE_NAME ;
		db.execSQL(DROP_TABLE);
		onCreate(db);		
	}
	
	// Adding new player
	public void addPlayer(Player player) 
	{
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(KEY_NAME, player.getName());
		values.put(KEY_PASSWORD, player.getPassword());
		
		// Insert row
		db.insert(TABLE_NAME, null, values);
		db.close(); // Closing the data base connection
	}
	 
	// Getting single player
	public Player getPlayer(String name) {
		
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_NAME, 
				new String[] { KEY_ID, KEY_NAME, KEY_PASSWORD},
				KEY_NAME + "=?",
				new String[]{ String.valueOf(name)},
				null,
				null,
				null,
				null
		       );
		
		if(cursor != null) cursor.moveToFirst();
		
		int player_id = Integer.parseInt(cursor.getString(0));
		String player_name = cursor.getString(1);
		String player_password = cursor.getString(2);
		
		Player player = new Player(player_id, player_name, player_password);
		
		return player;
		}
	 
	// Getting All players
	public List<Player> getAllPlayers() 
	{
		List<Player> playerList = new ArrayList<Player>();
		
		String SELECT_PLAYERS = "SELECT * FROM " + TABLE_NAME ;
		
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(SELECT_PLAYERS, null);
		
		if(cursor.moveToFirst())
		{
			do
			{
				int player_id = Integer.parseInt(cursor.getString(0));
				String player_name = cursor.getString(1);
				String player_password = cursor.getString(2);
				Player player = new Player(player_id, player_name, player_password);
				
				playerList.add(player);
				
			}while(cursor.moveToNext());
		}	
		
		return playerList;
	}
	 
	// Getting players Count
	public int getPlayersCount() 
	{
		String SELECT_PLAYERS = "SELECT * FROM " + TABLE_NAME ;
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.rawQuery(SELECT_PLAYERS, null);
		cursor.close();
		
		int player_number = cursor.getCount();
		
		return player_number;
		}
	// Updating single player
	public int updatePlayer(Player player) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(KEY_NAME, player.getName());
		values.put(KEY_PASSWORD, player.getPassword());
		
		int returnValue = db.update(TABLE_NAME, values, KEY_ID + "= ?",
		     new String[]{ String.valueOf(player.getId())});
		
		return returnValue;
	}
	 
	// Deleting single player
	public void deletePlayer(Player player) 
	{
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_NAME, KEY_ID + "= ?",
				new String[]{String.valueOf(player.getId())});
		db.close();
		
	}
}
























